package org.meichao.config.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.meichao.config.DataSourceContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * aop拦截设置本地线程变量
 */
@Aspect
@Component
public class DataSourceAop {

    private static Logger logger = LoggerFactory.getLogger(DataSourceAop.class);

    //本地线程
    private static final ThreadLocal<String> local = new ThreadLocal<>();

    /**
     * 数据源切换为读库
     */
    @Before("@annotation(org.meichao.config.annotation.Read)")
    public void setReadDataSourceType(){
        logger.info("dataSource切换到：Read");
        //local.set(DataSourceType.read.getType());
        DataSourceContextHolder.switch2Read();
    }

    /**
     * 数据源切换为写库
     */
    @Before("@annotation(org.meichao.config.annotation.Write)")
    public void setWriteDataSourceType(){
        logger.info("dataSource切换到：Write");
        //local.set(DataSourceType.write.getType());
        DataSourceContextHolder.switch2Write();
    }

}
